<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device-width, initial - scale = 1.0">
    <title>分期付款贷款计算器</title>

    <style>
        .box {
            width: 820px;
            height: 575px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);

        }

        .div1 {
            margin: 20px;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #f9f9f9;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            margin-bottom: 15px;
            padding: 5px;
            width: 280px;
        }

        .button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-left: 40%;
            margin-top: 15px;
        }

        .button:hover {
            padding: 10px 20px;
            background-color: #6f7e90;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .table {
            margin-top: 20px;
            border-collapse: "collapse";
            width: 95%;
            margin-left: 20px;
        }

        .th1,
        th {
            padding: 8px;
            text-align: left;

        }

        .th,
        td {
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="div1">
            <label for="loanAmount">贷款金额(元) : <input type="number" id="loanAmount" min="1000" max="1000000" required>
                (1,000 - 1,000,000之间的整数)</label>
            <label for="annualInterest">年化利率(%) : <input type="number" id="annualInterest" min="0" max="30" step="0.01"
                    required>
                (0 - 30之间的小数)</label>
            <label for="loanYears">还款年数(年) : <input type="number" id="loanYears" min="0" max="30" required>
                (0 - 30之间的整数)</label>
        </div>
        <button class="button" type="button" onclick="calculateLoan()">计算还款信息</button>

        <table class="table" id="paymentTable">
            <thead>
                <tr>
                    <th class="th">还款期数</th>
                    <th class="th">剩余欠款</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>

    <script>
        function calculateLoan() {
            const loanAmount = parseFloat(document.getElementById('loanAmount').value);
            const annualInterest = parseFloat(document.getElementById('annualInterest').value);
            const loanYears = parseInt(document.getElementById('loanYears').value);

            const monthlyInterest = annualInterest / 12 / 100;
            const numPayments = loanYears * 12;

            const monthlyPayment = loanAmount * (monthlyInterest * Math.pow(1 + monthlyInterest, numPayments)) / (Math.pow(1 + monthlyInterest, numPayments) - 1);

            const tbody = document.getElementById('paymentTable').getElementsByTagName('tbody')[0];

            let balance = loanAmount;
            for (let i = 1; i <= numPayments; i++) {
                const remainingBalance = loanAmount * (Math.pow(1 + monthlyInterest, numPayments) - Math.pow(1 + monthlyInterest, i)) / (Math.pow(1 + monthlyInterest, numPayments) - 1);
                const row = document.createElement('tr');
                const paymentNumberCell = document.createElement('td');
                paymentNumberCell.textContent = i;
                const balanceCell = document.createElement('td');
                balanceCell.textContent = remainingBalance.toFixed(3);
                row.appendChild(paymentNumberCell);
                row.appendChild(balanceCell);
                tbody.appendChild(row);
                balance = remainingBalance;
            }

            const monthlyPaymentCell = document.createElement('td');
            monthlyPaymentCell.textContent = monthlyPayment.toFixed(2);
            const paymentRow = document.createElement('tr');
            const paymentNumberCell2 = document.createElement('td');
            paymentNumberCell2.textContent = '每月还款金额';
            paymentRow.appendChild(paymentNumberCell2);
            paymentRow.appendChild(monthlyPaymentCell);

            const monthsRow = document.createElement('tr');
            const monthsLabelCell = document.createElement('td');
            monthsLabelCell.textContent = '还款月数';
            const monthsValueCell = document.createElement('td');
            monthsValueCell.textContent = (loanYears * 12) + '个月';
            monthsRow.appendChild(monthsLabelCell);
            monthsRow.appendChild(monthsValueCell);

            tbody.insertBefore(monthsRow, tbody.firstChild);
            tbody.insertBefore(paymentRow, tbody.firstChild);
        }
    </script>
</body>

</html>